package ca.nfb.media.nfbPlayer.views
{
	import ca.nfb.media.nfbPlayer.ComModule;
	import ca.nfb.media.nfbPlayer.ui.PlaylistImage;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.FullScreenEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import ca.turbulent.media.events.PyroEvent;
	
	public class FinalPlaylist extends NFBPlayerView
	{
		private var imagesCoords		:Array = new Array( {x:49, y:51}, {x:188, y:51}, {x:327, y:51}, {x:49, y:131}, {x:188, y:131}, {x:327, y:131} );
		public var imagesArray			:Array = new Array();
		public var imagesInPage			:Number = 0;
		public var maxImagesPerPage		:Number = 6;
		public var pageIndex			:Number = 1;
		public var wasDrawnEarlier		:Boolean = false;
		public var totalPages			:Number = 1;
		
	
		public function FinalPlaylist()
		{
			super();
		}
		
		override protected function addedToStage(evt:Event):void 
		{ 
			registerStageEvents();	
			this.x = this.y = 2;
			previousButton_mc.gotoAndStop(1);
			nextButton_mc.gotoAndStop(1);
			
			MovieClip(previousButton_mc).useHandCursor = true;
			MovieClip(nextButton_mc).useHandCursor = true;
			MovieClip(previousButton_mc).mouseEnabled = true;
			MovieClip(nextButton_mc).mouseEnabled = true;
			MovieClip(previousButton_mc).buttonMode = true;
			MovieClip(nextButton_mc).buttonMode = true;
			
			MovieClip(previousButton_mc).addEventListener(MouseEvent.MOUSE_OVER, mOver, false, 0, true);
			MovieClip(nextButton_mc).addEventListener(MouseEvent.MOUSE_OVER, mOver, false, 0, true);
			
			MovieClip(previousButton_mc).addEventListener(MouseEvent.MOUSE_OUT, mOut, false, 0, true);
			MovieClip(nextButton_mc).addEventListener(MouseEvent.MOUSE_OUT, mOut, false, 0, true);
			
			MovieClip(previousButton_mc).addEventListener(MouseEvent.CLICK, clicked, false, 0, true);
			MovieClip(nextButton_mc).addEventListener(MouseEvent.CLICK, clicked, false, 0, true);
			
			core.screenReaderManager.addAccess(previousButton_mc, "Previous items", "Items précédents"); 
			core.screenReaderManager.addAccess(previousButton_mc, "Next items", "Items suivants"); 
			
			ComModule.getInstance().addEventListener(ComModule.PLAYLIST_RECEIVED_EVENT, playlistReceived, false, 0, true);
			
			if (!core.embeddedMode)
			{
				callToAction_mc.visible = false;
				nfbLogo_mc.visible = false;
			}
			else
			{
				MovieClip(linkButton_mc).addEventListener(MouseEvent.CLICK, gotoLink, false, 0, true);
				MovieClip(linkButton_mc).useHandCursor = MovieClip(linkButton_mc).mouseEnabled = true;
			}
			
			this.x = (stage.stageWidth-this.width) / 2;
			this.y = ((stage.stageHeight-core.controllerHeight) - this.height) / 2;
			
			this.visible = false;
		}
		
		override protected function checkLang():void
		{
			switch(ComModule.getLang())
			{
				case "EN":
				callToAction_mc.gotoAndStop(1);
				break;
				
				case "FR":
				callToAction_mc.gotoAndStop(2);
				break;
			}
		}
		
		private function mOver(evt:MouseEvent):void
		{
			switch(MovieClip(evt.currentTarget).name)
			{
				case 'previousButton_mc':
				previousButton_mc.alpha = 1;
				break;
				
				case 'nextButton_mc':
				nextButton_mc.alpha = 1;
				break;
			}
		}
		
		private function mOut(evt:MouseEvent):void
		{
			switch(MovieClip(evt.currentTarget).name)
			{
				case 'previousButton_mc':
				pageIndex > 1 ? previousButton_mc.alpha = .5 : previousButton_mc.alpha = .1;
				break;
				
				case 'nextButton_mc':
				pageIndex < totalPages ? nextButton_mc.alpha = .5 : nextButton_mc.alpha = .1; 
				break;
			}
		}
		
		private function clicked(evt:MouseEvent):void
		{
			switch(MovieClip(evt.currentTarget).name)
			{
				case 'previousButton_mc':
				showPreviousPage();
				break;
				
				case 'nextButton_mc':
				showNextPage();
				break;
			}
		}
		
		private function gotoLink(evt:MouseEvent):void
		{
			
		}
		
		private function clearImages():void
		{
			for (var i:Number=0; i<imagesArray.length; ++i)
			{
				PlaylistImage(imagesArray[i]).clear();
				
				if (this.contains(imagesArray[i]))
					this.removeChild(imagesArray[i]);
			}
		}
		
		private function clearPlaylist():void
		{
			
		}
		
		private function drawGallery():void
		{
			core.tabManager.clear();
			previousButton_mc.alpha = nextButton_mc.alpha = .5;
			MovieClip(previousButton_mc).mouseEnabled = MovieClip(nextButton_mc).mouseEnabled = true;
			
			MovieClip(nextButton_mc).tabIndex = -1;
			MovieClip(nextButton_mc).tabEnabled = false;
			MovieClip(previousButton_mc).tabIndex = 1;
			MovieClip(previousButton_mc).tabEnabled = true;
	
			imagesInPage = getImagesInPage();
			if (pageIndex == 1)
			{
				previousButton_mc.alpha = .1;
				MovieClip(previousButton_mc).mouseEnabled = false;
				MovieClip(previousButton_mc).tabEnabled = false;
				MovieClip(previousButton_mc).tabIndex = -1;
				MovieClip(nextButton_mc).mouseEnabled = true;
				MovieClip(nextButton_mc).tabIndex = 1;
				MovieClip(nextButton_mc).tabEnabled = true;
			}
			else if (pageIndex == totalPages)
			{
				nextButton_mc.alpha = .1;
				MovieClip(nextButton_mc).mouseEnabled = false;	
				MovieClip(nextButton_mc).tabIndex = -1;
				MovieClip(nextButton_mc).tabEnabled = false;
			}
			
			for (var i:Number=0; i<imagesInPage; ++i)
			{
				var firstIndex:Number = getFirstIndex();
				var data:Object = ComModule.getInstance().latestFinalPlaylist[firstIndex+i];
				var tempImage:PlaylistImage = new PlaylistImage(data);
				imagesArray.push(tempImage);
				tempImage.x = imagesCoords[i].x;
				tempImage.y = imagesCoords[i].y;
				tempImage.tabEnabled = true;
				tempImage.tabIndex = i+1;
				
				addChild(tempImage);
			}
			
		}
		
		private function getFirstIndex():Number
		{
			return (pageIndex * maxImagesPerPage)-maxImagesPerPage;
		}
		
		private function getImagesInPage():Number 
		{
			if (totalPages == 1)
			{
				return ComModule.getInstance().latestFinalPlaylist.length;
			}
			else if (pageIndex == totalPages) 
			{
				var firstIndex:Number = getFirstIndex();
				var numImages:Number = ComModule.getInstance().latestFinalPlaylist.length - firstIndex;
				if (numImages == 0)
					numImages = 6;	
			
				return numImages;
			}
			else
			{
				return 6;
			}
		}
		
		private function playlistReceived(evt:Event):void
		{
			if (core.currentState == NFBPlayer.END_PLAYLIST_STATE)
			{
				this.visible = true;
				show();
				resetPlaylist();
			}
		}
		
		private function resetPlaylist():void
		{
			pageIndex = 1;
			title_mc.title_txt.htmlText = ComModule.getInstance().latestFinalPlaylistDescription;
			totalPages = Math.ceil(ComModule.getInstance().latestFinalPlaylist.length / maxImagesPerPage);
			
			if (totalPages == 1)
			{
				previousButton_mc.visible = nextButton_mc.visible = false;
				this.imagesInPage = ComModule.getInstance().latestFinalPlaylist.length;
			}
			else
			{
				previousButton_mc.visible = nextButton_mc.visible = true;
				this.imagesInPage = 6;
			}
			clearImages();
			drawGallery();
		}
		
		private function show():void
		{
			
		}
		
		public function showNextPage():void
		{
			pageIndex++;
			clearImages();
			drawGallery();
		}
		
		public function showPreviousPage():void
		{
			pageIndex--;
			clearImages();
			drawGallery();
		}
		
		override protected function streamStarted(evt:PyroEvent):void
		{
			// fetch playlist data now. 
		}
		
		override protected function coreStateUpdated(evt:Event=null):void 
		{ 
			if (core.currentState == NFBPlayer.END_PLAYLIST_STATE)
			{
				if (!wasDrawnEarlier)// if (player.time >= player.duration - .25 || wasDrawnEarlier)
				{
					if (stage.stageWidth >= core.minimalWidthForPanel && stage.stageHeight >= core.minimalHeightForPanel)
					{
						this.visible = true;
						show();
						resetPlaylist();
						wasDrawnEarlier = false;
					}
					else
					{
						this.visible = false;
						clearPlaylist();
					}
				}	
				else
				{
					this.visible = false;
					clearPlaylist();
				}
			}
			else
			{
				this.visible = false;
				clearPlaylist();
				
			}
		}
		
		override protected function fullscreenHandler(evt:FullScreenEvent):void
		{
			this.x = (stage.stageWidth-this.width) / 2;
			this.y = ((stage.stageHeight-core.controllerHeight) - this.height) / 2;
		}
		
		override protected function stageResized(evt:Event=null):void
		{
			var localWidth:Number = stage.stageWidth >= core.minimumWidth ? stage.stageWidth : core.minimumWidth;
			var localHeight:Number = stage.stageHeight >= core.minimumHeight ? stage.stageHeight : core.minimumHeight;
			this.x = (localWidth-this.width) / 2;
			this.y = ((localHeight-core.controllerHeight) - this.height) / 2;
			
		} 
		
	}
}


		